package sanshui.system.trade.factory.data;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.util.CollectionUtils;
import sanshui.system.trade.config.SpringContextHolder;
import sanshui.system.trade.pojo.StockInfoVo;

import java.math.BigDecimal;
import java.util.*;

@Data
@Slf4j
public class DataContainer implements Container {

    String code; // 数据容器类型:一般是具体的code
    String name;
    String startDate;
    String endDate;
    TradeMarketKey marketKey;
    TradeSystemType tradeSystemType;
    DataLevelKey type;
    DataMarketType market;
    StockInfoVo nowStockInfo;
    DataFactory dataFactory;
    List<StockInfoVo> hisStockInfo = new ArrayList<>();
    List<String> allCodeCache = new ArrayList<>();
    List<String> codeFilterCache = new ArrayList<>();
    BigDecimal minTradeNum = new BigDecimal("100");

    TradeDataContainer tradeDataContainer = TradeDataContainer.create();

    List<String> targetCode = new ArrayList<>();

    Map<String, StockInfoVo> allDataPerCycle = new HashMap<>(); // 存放一个周期的数据
    Map<String, List<StockInfoVo>> allData = new HashMap<>(); // 存放一个周期的数据
    int index = 0;

    public DataContainer(){

    }

    public DataContainer(List<String> targetCodes, TradeMarketKey tradeMarketKey, TradeSystemType tradeSystemType, DataMarketType market, DataFactory dataFactory, String minTradeNum){
        setAllCodeCache(targetCodes);
        setMarketKey(tradeMarketKey);
        setTradeSystemType(tradeSystemType);
        setMarket(market);
        setDataFactory(dataFactory);
        setMinTradeNum(new BigDecimal(minTradeNum));
    }

    @Override
    public boolean isRunning() {
        return false;
    }

    @Override
    public void stop() {

    }

    @Override
    public boolean hasNext() {
        return tradeDataContainer.next(targetCode, index,  startDate, endDate) != null;
    }

    @Override
    public Map<String, StockInfoVo> next() {
        Map<String, StockInfoVo> next = tradeDataContainer.next(targetCode, index, startDate, endDate);
        index++;
        return next;
    }

    @Deprecated
    public StockInfoVo nextOne(){
        if (DataLevelKey._BACK_TRADE_SINGLE.compareTo(this.getType()) == 0){
            if (hisStockInfo.size() == 0){
                List<StockInfoVo> hisStockInfoList = dataFactory.getHisStockInfo(this.getCode(), startDate, endDate);
                if (null == hisStockInfoList || hisStockInfoList.size() == 0){
                    log.warn("没有获取到信息, stockInfoVo: null");
                    return null;
                } else {
                    this.setHisStockInfo(hisStockInfoList);
                }
            }
            if (index >= hisStockInfo.size()){
                log.info("历史数据使用完成");
                return null;
            }
            nowStockInfo = hisStockInfo.get(index);
            index++;
            return nowStockInfo;
        } else {
            StockInfoVo stockInfoVo = dataFactory.next(this.getCode(), this.getType());
            if (null == stockInfoVo){
                log.warn("没有获取到信息, stockInfoVo: null");
                return null;
            }
            this.setNowStockInfo(stockInfoVo);
            log.debug("数据容器获取信息, stockInfoVo:{}", stockInfoVo);
            return stockInfoVo;
        }

    }

    @Override
    public List<String> getAllCodes() {
        return tradeDataContainer.allCodeCache;
    }

    @Override
    public DataMarketType getDataMarketType() {
        return getMarket();
    }

    @Override
    public TradeMarketKey getTradeMarket() {
        return this.marketKey;
    }
}
